package com.z.controller;

import com.z.pojo.PageBean;
import com.z.pojo.Users;
import com.z.service.UsersService;
import com.z.service.impl.UsersServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/manageUser")
public class ManageUsersServlet extends HttpServlet {
    private final UsersService usi = new UsersServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String nowpage = req.getParameter("nowpage");
        String eachcount = req.getParameter("eachcount");

        String ordurl = String.valueOf(req.getRequestURL());
        String url = ordurl+"?"+(req.getQueryString()==null?"":req.getQueryString());
        if(url.contains("&nowpage")){
            url = url.substring(0,url.indexOf("&nowpage"));
        }

        if(nowpage == null||"".equals(nowpage)){
            nowpage ="1";
        }
        if(eachcount == null||"".equals(eachcount)){
            eachcount ="4";
        }
        PageBean<Users> pageBean = usi.manageUsers(Integer.valueOf(nowpage), Integer.valueOf(eachcount), null);

        pageBean.setUrl(url);
        req.setAttribute("upage",pageBean);
        req.getRequestDispatcher("/admin/manage/user.jsp").forward(req,resp);
    }
}
